
//验证码倒计时
var countdown=120;
function settime(obj) {
if (countdown == 0) {
// obj.removeAttribute("disabled");
obj.value="获取验证码";
countdown = 120;
return;
} else {
    // obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
var arr = [];
//绑定手机号
//点击出现绑定
$('.bind-mobile-btn').click(function(){
    $('.mobile-cover').css('display','block');
    $('.mobile-main').css('display','block');
});
$('.modified-mobile').click(function(){
    $('.mobile-cover').css('display','block');
    $('.mobile-main').css('display','block');
});
//点击阴影，消失
$('.mobile-cover').click(function(e){
    $('.mobile-cover').css('display','none');
    $('.mobile-main').css('display','none');
});

//验证手机号是否被绑定
$('.mobile').blur(function(){
    var mobile = $('.mobile').val();
    $.ajax({
        url:'/user/check_mobile.json',//需要加入验证请求地址
        type:'get',
        data:{
            mobile:mobile
        },
        dataType:'text',
        success:function(data){
            console.log(data);
            //需要返回这个手机号是否已被绑定过的状态state
            if(data.isExist==true){//已绑定
                $('.err-info').html('手机号可以使用');
                //点击发送验证码
                $('.to-code').click(function(){
                    var mobile = $('.mobile').val();
                    var th = this;
                    if(!mobile){
                        $('.err-info').html('手机号不能为空');
                    }else{
                        $.ajax({
                            url:'/bind-mobile-code.json',
                            type:'get',
                            data:{
                                mobile:mobile
                            },
                            dataType:'text',
                            success:function(data){
                                settime(th);
                                $('.err-info').html('验证码已发送');
                            },
                            error:function(err){
                                $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
                            }
                        });
                    }
                });
                //点击进行绑定
                $('.bind-btn').click(function(){
                    var mobile = $('.mobile').val();
                    var code = $('.code').val();
                    if(!mobile){
                        $('.err-info').html('手机号不能为空');
                    }else if(!code){
                        $('.err-info').html('验证码不能为空');
                    }else{
                        $.ajax({
                            url:'/bind-mobile.json',
                            type:'get',
                            data:{
                                mobile:mobile,
                                code:code
                            },
                            dataType:"json",
                            success:function(data){
                                console.log(data);
                                $('.err-info').html('绑定成功！');
                                setTimeout(function(){
                                    $('.mobile-cover').css('display','none');
                                    $('.mobile-main').css('display','none');
                                    location.reload();
                                },500);

                            },
                            error:function(err){
                                $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
                            }
                        });
                    }
                });
            }else{
                if(mobile==''){
                    $('.err-info').html('手机号不能为空');
                }else{
                    $('.err-info').html('手机号已被绑定，请更换');
                }
            }
        },
        error:function(err){
            $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
        }
    });
});
//邮箱发送成功提示
$('.to-send-email').click(function(){
    $('.email-cover').css('display','block');
});
$('.email-cover').click(function(){
    $('.email-cover').css('display','none');
});
//邮箱验证
$('.to-send-email').click(function(){
    $.ajax({
        url:'/user/send_activate_mail.json',
        type:'get',
        dataType:"json",
        success:function(data){
            $('.email-cover').css('display','block');
            $('.email-main').css('display','block');
        },
        error:function(err){
            $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
        }
    });
});

$('.email-ok').click(function(){
    $('.email-cover').css('display','none');
    $('.email-main').css('display','none');
});

//解除谷歌验证
$('.remove').click(function(){
    $('.remove-goo').css('display','block');
});
$('.remove-btn').click(function(){
    var secretCode = $('.g-password').val();
    var reg=/^\d{6}$/;
    if(reg.test(secretCode)){ //是6位数字
        $.ajax({
            url:'/user/cancel_google_secret.json',
            type:'patch',
            dataType:'json',
            data:{
                secretCode:secretCode
            },
            success:function(data){
                $('.err-info').html('解除成功');
                setTimeout(function(){
                    location.reload();
                },200);
            },
            error:function(msg){
                $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
            }
        });
    }else{
        $('.err-info').html('请输入验证器上的6位数字密码');
    }
});